package service

import (
	"gitee.com/ling1314/origin-utility/miao_ser"
	"gitee.com/ling1314/origin-utility/model"
	"gitee.com/ling1314/origin-utility/modules/repository"
)

type hostService struct {
	baseService
}

func (ser hostService) Details(hostId int64) (rs model.HostDetail, err error) {
	var (
		h model.AppHost
	)
	if h, err = repository.HostRepository.FindById(hostId); err != nil {
		return
	}
	rs.AppHost = h
	if miao_ser.IsOnline(hostId) {
		rs.IsOnline = 1
	} else {
		rs.IsOnline = 2
	}
	return
}
func (ser hostService) List() (rs []model.HostStatus, err error) {
	var (
		li []model.AppHost
	)
	if li, _, err = repository.HostRepository.FindAll(); err != nil {
		return
	}
	rs = make([]model.HostStatus, len(li))
	for i := range li {
		rs[i].AppHost = li[i]
		if miao_ser.IsOnline(li[i].HostId) {
			rs[i].IsOnline = 1
		} else {
			rs[i].IsOnline = 2
		}
	}
	return
}

func (ser hostService) Page() (result model.PageResult, err error) {
	var (
		li    []model.AppHost
		total int64
	)
	if li, total, err = repository.HostRepository.FindAll(); err != nil {
		return
	}
	result.List = li
	result.Total = total
	return result, nil
}
